﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using BackgrounderService;

namespace HaveARest
{
    public class RestInfo
    {
        public int defaulMaxBusyTime = 10;
        public int defaulBusySpan = 300;
        public int maxBusyTime;
        public int busySpan;
        public bool enabled = true;
    }

    public class tcT
    {
        public tcFormcs tc;

        public tcT(tcFormcs t)
        {
            tc = t;
        }

        public void tcStart()
        {
            Application.Run(tc);
        }
    }

    public class Rest : BackgrounderService.IPlugin
    {
        public void Start()
        {
            System.Windows.Forms.MessageBox.Show("start");
            running = true;
            lastOperateTime = DateTime.Now;
            busyTime = new TimeSpan(0);
            timer.Start();
            if (!info.enabled)
            {
                System.Windows.Forms.MessageBox.Show("你必须先在设置中启用该插件！");
                return;
            }
        }

        public void Stop()
        {
            System.Windows.Forms.MessageBox.Show("stop");
            busyTime = new TimeSpan(0);
            running = false;
        }

        public void timerCall(object source, System.Timers.ElapsedEventArgs e)
        {
            if ((DateTime.Now - lastOperateTime).TotalMilliseconds < info.busySpan * 1000)
            {
                if (tc.showState == 0)
                {
                    tcThread = new Thread(new ThreadStart(temp.tcStart));
                    tcThread.Start();
                }
                busyTime = new TimeSpan(0);
            }
        }

        public void MouseCall(BackgrounderService.MouseEventArgs mouseEvent)
        {
            if (running)
            {
                lock (restLock)
                {
                    DateTime thisOperateTime = DateTime.Parse(mouseEvent.Time);
                    if ((thisOperateTime - lastOperateTime).TotalMilliseconds > info.busySpan * 1000)
                    {
                        busyTime = new TimeSpan(0);
                        timer = new System.Timers.Timer(info.maxBusyTime * 1000);
                        timer.Elapsed += new System.Timers.ElapsedEventHandler(timerCall);
                        timer.AutoReset = true;
                        timer.Start();
                    }
                    else
                    {
                        //busyTime += thisOperateTime - lastOperateTime;
                        //if (busyTime.TotalMilliseconds >= info.maxBusyTime * 1000)
                        //{
                        //    if (tc.showState == 0)
                        //    {
                        //        tcThread = new Thread(new ThreadStart(temp.tcStart));
                        //        tcThread.Start();
                        //    } 
                        //    busyTime = new TimeSpan(0);
                        //}
                    }
                    lastOperateTime = thisOperateTime;
                }
            }
        }

        public void KeyCall(BackgrounderService.KeyEventArgs keyEvent)
        {
            if (running)
            {
                lock (restLock)
                {
                    DateTime thisOperateTime = DateTime.Parse(keyEvent.Time);
                    if ((thisOperateTime - lastOperateTime).TotalMilliseconds > info.busySpan * 1000)
                    {
                        busyTime = new TimeSpan(0);
                        timer = new System.Timers.Timer(info.maxBusyTime * 1000);
                        timer.Elapsed += new System.Timers.ElapsedEventHandler(timerCall);
                        timer.AutoReset = true;
                        timer.Start();
                    }
                    else
                    {
                        //busyTime += thisOperateTime - lastOperateTime;
                        //if (busyTime.TotalMilliseconds >= info.maxBusyTime * 1000)
                        //{
                        //    if (tc.showState == 0)
                        //    {
                        //        tcThread = new Thread(new ThreadStart(temp.tcStart));
                        //        tcThread.Start();
                        //    } 
                        //    busyTime = new TimeSpan(0);
                        //}
                    }
                    lastOperateTime = thisOperateTime;
                }
            }
        }

        public void Setting()
        {
            //System.Windows.Forms.MessageBox.Show("setting");
            try
            {
                var Dialog = new RestSetting(info);
                Dialog.SetData();
                //var Dialog = new RestSetting();
                Dialog.ShowDialog();
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }

        public KeyValuePair<string, SettingHandler>? GetSettingCallBack()
        {
            KeyValuePair<string, SettingHandler> p = new KeyValuePair<string, SettingHandler>("HaveARest", this.Setting);
            return p;
        }

        public void Initialization(HookProxy hp)
        {
            P = new Properties.Settings();
            hp.RegisterMouse(this.MouseCall);
            hp.RegisterKey(this.KeyCall);
            running = false;
            info = new RestInfo();
            info.maxBusyTime = P.MaxBusyTime;
            info.busySpan = P.BusySpan;
            temp = new tcT(tc);
            timer = new System.Timers.Timer(info.maxBusyTime * 1000);
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timerCall);
            timer.AutoReset = true;
        }

        public string Name
        {
            get { return "HaveARest"; }
        }

        public void Unload()
        {
            P.MaxBusyTime = info.maxBusyTime;
            P.BusySpan = info.busySpan;
        }

        State IPlugin.state
        {
            get
            {
                if (running) return State.Start;
                else return State.Stop;
            }
        }

        private Properties.Settings P;
        public RestInfo info;
        bool running;
        DateTime lastOperateTime;
        TimeSpan busyTime;
        private static object restLock = new object();
        System.Timers.Timer timer;
        tcFormcs tc = new tcFormcs();
        tcT temp;
        Thread tcThread;
    }
}
